package com.hz.keyboard.widget;

import android.app.Dialog;
import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.inputmethodservice.KeyboardView.OnKeyboardActionListener;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import com.hz.keyboard.R;


/**
 * 车牌号填写控件
 * @author 李蔚翔
 */
public class CarnoInputDialog {

    /** 键盘 */
    private KeyboardView keyboardview;

    private Context context;
    /** 弹出的键盘dialog */
    private Dialog dialog;
    /** 接口 用于向外界传值 */
    private OnCarnoInputListener onCarnoInputListener;
    /** 返回的车牌号码 */
    private String finalCarno = "";
    /** 车牌号最大长度 每次输入的数量*/
    private int maxLength;

    /**
     * 接口,回传车牌号码
     * @author wk
     */
    public interface OnCarnoInputListener {
        public void back(String content);
        public void delText();
    }

    public CarnoInputDialog(Context context, OnCarnoInputListener onCarnoInputListener, String carno) {
        this.context = context;
        this.onCarnoInputListener = onCarnoInputListener;
        this.finalCarno = carno;
    }

    public CarnoInputDialog(Context context, OnCarnoInputListener onCarnoInputListener) {
        this.context = context;
        this.onCarnoInputListener = onCarnoInputListener;
    }

    public CarnoInputDialog(Context context) {
        this.context = context;
    }

    public void setOnCarnoInputListener(OnCarnoInputListener onCarnoInputListener){
        this.onCarnoInputListener = onCarnoInputListener;
    }


    public CarnoInputDialog builder(final int keyNo) {

        View view = LayoutInflater.from(context).inflate(R.layout.view_dialog_carno_input, null);

        /**
         * 获取键盘控件,并设置按键点击事件
         */
        keyboardview = (KeyboardView) view.findViewById(R.id.mykeyboardview);
        if(keyNo == 0){
            finalCarno = "";
            maxLength = 1;
            keyboardview.setKeyboard(new Keyboard(context, R.layout.keycontent_province));
        }else if(keyNo == 1){
            maxLength = 1;
            finalCarno = "";
            keyboardview.setKeyboard(new Keyboard(context, R.layout.keycontent_abcs));
        }else if(keyNo == 2){
            maxLength = 5;
            finalCarno = "";
            keyboardview.setKeyboard(new Keyboard(context, R.layout.keycontent_abcs));
        }else if(keyNo == 3){
            maxLength = 7;
            finalCarno = "";
            keyboardview.setKeyboard(new Keyboard(context, R.layout.keycontent_province));
        }

        keyboardview.setEnabled(true);
        keyboardview.setPreviewEnabled(true);
        keyboardview.setOnKeyboardActionListener(new OnKeyboardActionListener() {
            @Override
            public void onKey(int primaryCode, int[] keyCodes) {
                if (primaryCode == Keyboard.KEYCODE_CANCEL) {
                    dialog.dismiss();
                    return;
                } else if (primaryCode == Keyboard.KEYCODE_DELETE) {
                    if (finalCarno.length() > 0) {
                        finalCarno = finalCarno.substring(0, finalCarno.length() - 1);
                    }
                    if(keyNo == 2){   //删除车牌号码
                        if(onCarnoInputListener != null){
                            onCarnoInputListener.delText();
                        }
                        return;
                    }
                }else if(primaryCode == 10000 && keyNo == 3){
                    keyboardview.setKeyboard(new Keyboard(context, R.layout.keycontent_abcs));
                } else if(primaryCode == 10001 && keyNo == 3){
                    keyboardview.setKeyboard(new Keyboard(context, R.layout.keycontent_province));
                } else if(primaryCode == 10001 || primaryCode == 10000){
                } else{
//                    finalCarno += Character.toString((char) primaryCode);
                    finalCarno = Character.toString((char) primaryCode);
                    if (primaryCode > 10000) {
                        keyboardview.setKeyboard(new Keyboard(context, R.layout.keycontent_abcs));
                    }
                }

//                if (finalCarno.length() > maxLength) {
//                    finalCarno = finalCarno.substring(0, maxLength);
//                }

                if(keyNo == 0 && finalCarno.length() ==1){
                    dialog.dismiss();
                }else if(keyNo == 1 && finalCarno.length() ==1){
                    dialog.dismiss();
                }else if(keyNo == 2 && finalCarno.length() ==5){
                    dialog.dismiss();
                }else if(keyNo == 3 && finalCarno.length() ==7){
                    dialog.dismiss();
                }

                if(onCarnoInputListener != null){
                    onCarnoInputListener.back(finalCarno);
                }
            }

            @Override
            public void onPress(int primaryCode) {
            }
            @Override
            public void onRelease(int primaryCode) {
            }
            @Override
            public void onText(CharSequence text) {
            }
            @Override
            public void swipeDown() {
            }
            @Override
            public void swipeLeft() {
            }
            @Override
            public void swipeRight() {
            }
            @Override
            public void swipeUp() {
            }
        });

        /**
         * dialog样式
         */
        dialog = new Dialog(context, R.style.CarnoInputDialogStyle);
        dialog.setContentView(view);

        /**
         * 设置dialog从底部弹出 宽度填满屏幕
         */
        Window dialogWindow = dialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager m = dialogWindow.getWindowManager();
        /** 获取屏幕宽、高用 */
        Display d = m.getDefaultDisplay();
        /** 获取对话框当前的参数值 */
        WindowManager.LayoutParams p = dialogWindow.getAttributes();
        /** 宽度设置为屏幕的100% */
        p.width = (int) (d.getWidth());
        dialogWindow.setAttributes(p);

        return this;
    }

    public void show() {
        if (dialog != null) {
            dialog.show();
        }
    }

    public void hide() {
        if (dialog != null) {
            dialog.dismiss();
        }
    }
}
